package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.Goods;
import com.example.entity.store;
import com.example.service.impl.AdminServiceImpl;
import com.example.service.GoodsService;
import com.example.service.storeService;
import com.example.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Goods")
public class GoodsController {

    @Autowired
    private GoodsService GoodsService;

    @Autowired
    AdminServiceImpl adminServiceImpl;

    @Autowired
    private storeService storeService;


    /**
     * 根据 店铺id查询商品
     * @param name
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public Result getById(@RequestParam(required = false) String name,String id){
        //查询店铺  获取商品id
        store store = storeService.getById(id);

        List<Goods> list = new ArrayList<>();

        String goods = store.getGoods();
        String[] split = goods.split(",");
        //根据商品id 进行查询
        for (String s : split) {
            //只查询上架的商品
            Goods byId = GoodsService.getOne(new QueryWrapper<Goods>().eq("id",s).eq("status",1));
            list.add(byId);
        }

        //返回数据
        return Result.success(list);
    }


    @GetMapping("/allList")
    public Result allList(){
        Account currentUser = TokenUtils.getCurrentUser();
        Integer id = currentUser.getId();
        return Result.success(GoodsService.list( new QueryWrapper<Goods>().eq("business",id)));
    }

    @GetMapping("/list")
    public Result list(@RequestParam("pageNo") int pageNo,@RequestParam("pageSize") int pageSize,@RequestParam(required = false) String name){
        Account currentUser = TokenUtils.getCurrentUser();
        Integer id = currentUser.getId();
        Page<Goods> page = GoodsService.page(new Page<Goods>(pageNo, pageSize), new QueryWrapper<Goods>().eq("business",id).like(name != null,"name",name));
        for (Goods record : page.getRecords()) {
            Admin admin = adminServiceImpl.selectById(Integer.valueOf(record.getBusiness()));
            record.setBusiness(admin.getName());
        }
        return Result.success(page);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Goods Goods){
        Account currentUser = TokenUtils.getCurrentUser();
        Integer id = currentUser.getId();
        Goods.setBusiness(String.valueOf(id));
        GoodsService.save(Goods);
        return Result.success();
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody Goods Goods){
        GoodsService.updateById(Goods);
        return Result.success();
    }

    @DeleteMapping("/remove/{id}")
    public Result removeByid(@PathVariable int id){
        GoodsService.removeById(id);
        return Result.success();
    }

}
